package com.llc.gulimall.seckill.controller;

import com.llc.common.utils.R;
import com.llc.gulimall.seckill.service.SeckillService;
import com.llc.gulimall.seckill.to.SeckillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author wangwei
 * 2021/1/30 19:50
 */
@Slf4j
@Controller
//@RequestMapping("/seckill")
public class SeckillController {

    @Autowired
    SeckillService seckillService;


    /**
     *
     * @author llc
     * @description  查询当前场次的秒杀信息
     * @date 2022/2/20 14:57
     * @param
     * @return com.llc.common.utils.R
     */
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus(){
        log.info("查询秒杀商品信息");
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        log.info("查询秒杀商品信息结束：vos"+vos);
        return R.ok().setData(vos);
    }
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        long time = new Date().getTime();
        log.info("time:"+time);
        return R.ok().setData(to);
    }


    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                     @RequestParam("key") String key,
                     @RequestParam("num") Integer num,
                          Model model){
        log.info("开始执行秒杀");
        String orderSn = seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }

    //@ResponseBody
    //@GetMapping("/current")
    //public R getCurrentSeckillSessions() {
    //    List<SeckillSkuRedisTo> skus = seckillService.currentSeckill();
    //    return R.ok().setData(skus);
    //}

    //@ResponseBody
    //@GetMapping("/sku/{skuId}")
    //public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId) {
    //    SeckillSkuTO seckillSkuTO = seckillService.getSkuSeckillInfo(skuId);
    //    return R.ok().setData(seckillSkuTO);
    //}

    /**
     * 秒杀下单
     * @param seckillId
     * @param seckillToken
     * @param count
     * @param model
     * @return
     */
    //@GetMapping("/item")
    //public String seckillItem(@RequestParam("seckillId") String seckillId,
    //                     @RequestParam("seckillToken") String seckillToken,
    //                     @RequestParam("count") Integer count,
    //                     Model model)  {
    //    // 秒杀成功，订单号
    //    String orderSn = seckillService.seckillItem(seckillId, seckillToken, count);
    //    model.addAttribute("orderSn", orderSn);
    //    return "success";
    //}
}
